from os import path

import jieba

from PIL import Image

import numpy as np

import matplotlib.pyplot as plt

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

d = path.dirname(__file__)

# Read the whole text.

text = open(path.join(d, 'text/chinaHistory.txt'), 'r', encoding='utf-8').read()

textlist = jieba.lcut(text)

words = " ".join(textlist)
print(words)
# read the mask image

# taken from

# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg

alice_mask = np.array(Image.open(path.join(d, "img/ditu.jpg")))

wc = WordCloud(font_path='真宗圣典明朝.otf', background_color='white', max_words=2000, mask=alice_mask,
               max_font_size=40, random_state=42, scale=10)

# generate word cloud

wc.generate(words)

wc.to_file(path.join(d, "china.png"))

image_colors = ImageColorGenerator(alice_mask)

# plt.imshow(wc)

# plt.axis('off')

# plt.figure()
#
plt.imshow(wc.recolor(color_func=image_colors))
# #
plt.axis('off')
#
# plt.figure()
#
# plt.imshow(alice_mask, cmap=plt.cm.gray)
#
# plt.axis("off")

plt.show()
